#!/bin/bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export LD_LIBRARY_PATH=$SNAP_LIBRARY_PATH:$LD_LIBRARY_PATH

source $SNAP/actions/common/utils.sh

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
  echo "api service kicker will not run on a cluster node"
  exit 0
fi

restart_attempt=0
installed_registry_help=0
while true
do
    if [ $restart_attempt -ge 5 ]
    then
        echo "Service kicker restarted the apiserver too quickly. Exiting."
        exit 1
    fi

    # every 3 seconds
    sleep 3
    if ! grep -E "(--advertise-address|--bind-address)" $SNAP_DATA/args/kube-apiserver &> /dev/null &&
       ip route | grep default &> /dev/null &&
       snapctl services microk8s.daemon-apiserver | grep active &> /dev/null
    then
        csr_modified="$(produce_certs)"
        if [[ "$csr_modified" -eq "1" ]];
        then
            echo "CSR change detected. Reconfiguring the kube-apiserver"
            rm -rf .srl
            snapctl restart microk8s.daemon-etcd.service
            snapctl restart microk8s.daemon-containerd.service
            snapctl restart microk8s.daemon-apiserver.service
            snapctl restart microk8s.daemon-proxy.service
            snapctl restart microk8s.daemon-kubelet.service
            restart_attempt=$[$restart_attempt+1]
        else
            restart_attempt=0
        fi
    fi

    # If no local-registry-hosting documentation has been installed,
    # install a help guide that points to the microk8s registry docs.
    if [ $installed_registry_help -eq 0 ] &&
       snapctl services microk8s.daemon-apiserver | grep active &> /dev/null
    then
      if ! "$SNAP/kubectl" "--kubeconfig=$SNAP_DATA/credentials/client.config" get configmap local-registry-hosting -n kubepublic
      then
        use_manifest registry-help apply
      fi
      
      installed_registry_help=1
    fi
done
